﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Xml.Serialization;
using System.Text;

public static class Tools
{
    /// <summary>
    /// 所有面板的父物体
    /// </summary>
    private static Transform uiParent;
    public static Transform UIParent
    {
        get
        {
            return GameObject.Find("GameRoot").transform;
        }
    }

    /// <summary>
    /// 创建面板
    /// </summary>
    /// <param name="panelType">面板的类型</param>
    /// <returns>创建面板的实例</returns>
    public static GameObject CreateUIPanel(PanelType panelType)
    {
        GameObject prefab = Resources.Load<GameObject>(panelType.ToString());
        if (prefab == null)
        {
            Debug.LogWarning("没有找到" + panelType.ToString() + "面板");
            return null;
        }
        else
        {
            GameObject panel = Object.Instantiate(prefab);
            panel.name = panelType.ToString();//设置面板的名字不然会带clone
            panel.transform.SetParent(UIParent, false);
            return panel;
        }
    }
    /// <summary>
    /// 以UTF8格式保存数据
    /// </summary>
    public static void SaveDate(GameData data)
    {
        Stream stream = new FileStream(Consts.DataPath, FileMode.OpenOrCreate, FileAccess.Write);
        StreamWriter sw = new StreamWriter(stream, Encoding.UTF8);//保存数据流
        XmlSerializer xmlSerializer = new XmlSerializer(data.GetType());
        //序列化
        xmlSerializer.Serialize(sw, data);

        sw.Close();
        stream.Close();
    }
    /// <summary>
    /// 获取数据
    /// </summary>
    public static GameData GetDataOutBOM()
    {
        GameData data = new GameData();

        Stream stream = new FileStream(Consts.DataPath, FileMode.Open, FileAccess.Read, FileShare.None);
        //忽略BOM标记 -- true
        StreamReader sr = new StreamReader(stream, true);//读数据

        XmlSerializer xmlSerialize = new XmlSerializer(data.GetType());
        //反序列化
        data = xmlSerialize.Deserialize(sr) as GameData;
        stream.Close();
        sr.Close();
       
        return data;
    }
    /// <summary>
    /// 排序
    /// </summary>
    /// <param name="cradList">要排序的牌</param>
    /// <param name="asc">升序</param>
    public static void Sort(List<Card> crads, bool asc)
    {
        crads.Sort(
            (Card a, Card b) =>
            {
                if (asc)
                {
                    return a.CardWeight.CompareTo(b.CardWeight);
                }
                else
                {
                    return -a.CardWeight.CompareTo(b.CardWeight);
                }
            }
            );
    }
    /// <summary>
    /// 得到权重
    /// </summary>
    /// <param name="cards">所选牌</param>
    /// <param name="cType">牌型</param>
    /// <returns></returns>
    public static int GetWeight(List<Card> cards, CardType cType)
    {
        int currentWeight = 0;
        //如果是三带一或者三带二
        if (cType == CardType.Three|| cType == CardType.ThreeAndOne || cType == CardType.ThreeAndTwo)
        {
            for (int i = 0; i < cards.Count-2; i++)
            {
                if (cards[i].CardWeight == cards[i + 1].CardWeight && cards[i + 1].CardWeight == cards[i + 2].CardWeight)
                {
                    currentWeight = (int)cards[i].CardWeight;
                    break;
                }
            }
        }
        //如果是顺子
        else if (cType == CardType.Straight || cType == CardType.DoubleStraight || cType == CardType.TripleStraight)
        {
            currentWeight = (int)cards[0].CardWeight;
        }
        //其他牌型全算权值
        else
        {
            for (int i = 0; i < cards.Count; i++)
            {
                currentWeight += (int)cards[i].CardWeight;
            }
        }
        return currentWeight;
    }
}
